Skip to content

Latest commit

 

History

History
 
 

05 Qt Designer Python

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Qt Designer Python

Qt Designer is a graphical tool for building Qt GUIs:

Qt Designer screenshot on Windows

It produces .ui files. You can load these files from C++ or Python to display the GUI.

The dialog in the following screenshot comes from the file dialog.ui in this directory:

Qt Designer Python

The main.py script (also in this directory) loads and invokes dialog.ui from Python. The steps with which it does this are quite easy.

First, main.py imports the uic module from PyQt5:

from PyQt5 import uic

It also imports QApplication. Like all (Py)Qt apps, we must create an instance of this class.

from PyQt5.QtWidgets import QApplication

Then, we use uic.loadUiType(...) to load the .ui file. This returns two classes, which we call Form and Window:

Form, Window = uic.loadUiType("dialog.ui")

The first is an ordinary Python class. It has a .setupUi(...) method which takes a single parameter, the widget in which the UI should be displayed. The type of this parameter is given by the second class, Window. This is configured in Qt Designer and is usually one of QDialog, QMainWindow or QWidget.

To show the UI, we thus proceed as follows. First, we create the necessary QApplication:

app = QApplication([])

Then, we instantiate the Window class. It will act as the container for our user interface:

window = Window()

Next, we instantiate the Form. We invoke its .setupUi(...) method, passing the window as a parameter:

form = Form()
form.setupUi(window)

We've now connected the necessary components for displaying the user interface given in the .ui file. All that remains is to .show() the window and kick off Qt's event processing mechanism:

window.show()
app.exec_()

For instructions how to run this example yourself, please see here.